<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');


function _cron_custom_compare($c1, $c2)
{
  return $c1->start > $c2->start;
}

class Cron extends CI_Controller
{

  function __construct()
  {
    parent::__construct();

    $this->load->model('event_model');
    $this->load->model('schedule_model');
    $this->load->model('user_model');

    date_default_timezone_set('Australia/Victoria');
  }

  function run($seret = '')
  {
    if($seret !== 'woodkey98')
    {
      show_error('Access denied');
    }

    // cleaning up events + schedules that doesn't have products or location associated to them
    $sql = "DELETE FROM schedules
WHERE eid IN (SELECT eid FROM events e
WHERE e.product_id NOT IN (SELECT pid FROM products))";
    $this->db->query($sql);
    $this->db->query("DELETE FROM events WHERE product_id NOT IN (SELECT pid FROM products)");

    //cleaning up events + schedules that doesn't have students or teachers associated to theme
    $sql = "DELETE FROM schedules WHERE eid IN (SELECT eid FROM events e WHERE e.student_id NOT IN (SELECT sid FROM students))";
    $this->db->query($sql);
    $this->db->query("DELETE FROM events WHERE student_id NOT IN (SELECT sid FROM students)");

    // set all pass schedules to passed
    $sql = "
      UPDATE schedules s SET s.status = ?
      WHERE s.end <= ? AND s.status = ?
    ";
    $this->db->query($sql, array(
      Schedule_model::STATUS_PASSED,
      time(),
      Schedule_model::STATUS_PENDING
    ));

    // set all events to inactive if all schedules passed
    $event_ids = array();
    $sql = "
      SELECT e.eid, (
        SELECT COUNT(*)
        FROM schedules AS s
        WHERE s.eid = e.eid
      ) AS cnt, (
        SELECT COUNT(*)
        FROM schedules AS s
        WHERE s.eid = e.eid AND s.status != " . Schedule_model::STATUS_PENDING . "
      ) AS cnt1
      FROM events as e
      WHERE e.status = " . Event_model::STATUS_ACTIVE . "
    ";
    $result = $this->db->query($sql)->result();
    foreach($result as $row)
    {
      if($row->cnt == $row->cnt1)
      {
        $this->db->query("UPDATE events e SET e.status = ? WHERE e.eid = ?", array(
          Event_model::STATUS_INACTIVE, $row->eid
        ));
      }
      $event_ids[] = $row->eid;
    }

    // process sending emails, 11AM every morning (set in config file)
    $current_hour = date('G');
    if($current_hour == $this->config->item('voxcrm_mail_time'))
    {
      foreach($event_ids as $eid)
      {
        $event = $this->event_model->events($eid);
        $event->schedules = $this->schedule_model->event_schedules($eid);

        usort($event->schedules, "_cron_custom_compare");

        // second last emails
        $second_last_index = count($event->schedules) - 2 >= 0 ? count($event->schedules) - 2 : 0;
        $second_last_schedule = $event->schedules[$second_last_index];

        if($second_last_schedule->start > strtotime('0:00') && $second_last_schedule->start < strtotime('0:00') + 86400)
        {
          $student = $this->user_model->student_info($event->student_id);

          my_send_mail(
            array(
              'to' => $student->mail,
              'subject' => "COURTESY REMINDER | {$this->config->item('school_name')}",
              'message' => "
<p>Hi {$student->first_name},</p>
<p>Just a courtesy reminder that you have 2 lessons remaining before the end of your term.
To secure your time slot, please ensure your payment is received 72 hours after your last paid class.
If we have not received your payment 72 hours after your last paid class you will unfortunately forfeit your time slot.</p>

<p>Thank you!</p>
<p><strong>{$this->config->item('school_name_long')}</strong></p>
              "
            )
          );
        }

        // 72 hours (3 days) before the last class
        $first_last_index = count($event->schedules) - 1 >= 0 ? count($event->schedules) - 1 : 0;
        $first_last_schedule = $event->schedules[$first_last_index];
        if(($first_last_schedule->start - 3*86400) > strtotime('0:00') &&
          (($first_last_schedule->start - 3*86400) < strtotime('0:00') + 86400))
        {
          $student = $this->user_model->student_info($event->student_id);

          my_send_mail(
            array(
              'to' => $student->mail,
              'subject' => "COURTESY REMINDER | {$this->config->item('school_name')}",
              'message' => "
<p>Hi {$student->first_name},</p>
<p>We just wanted to let you know that your current term is due to be completed in 3 days.</p>
<p>Thank you, </p>
<p><strong>{$this->config->item('school_name_long')}</strong></p>
              "
            )
          );
        }

        // 24 hours (1 day) before the last class
        if(($first_last_schedule->start - 1*86400) > strtotime('0:00') &&
          (($first_last_schedule->start - 1*86400) < strtotime('0:00') + 86400))
        {
          $student = $this->user_model->student_info($event->student_id);

          my_send_mail(
            array(
              'to' => $student->mail,
              'subject' => "COURTESY REMINDER | {$this->config->item('school_name')}",
              'message' => "
<p>Hi {$student->first_name},</p>
<p>We just want to let you know that your current term is due to be completed in 1 day.</p>
<p>To secure your time slot, please ensure your payment is received 72 hours after your last paid class.
If we have not received your payment 72 hours after your last paid class you will unfortunately forfeit your time slot.</p>
<p>Thank you, </p>
<p><strong>{$this->config->item('school_name_long')}</strong></p>
              "
            )
          );
        }

        // last emails
        if($first_last_schedule->start > strtotime('0:00') && $first_last_schedule->start < strtotime('0:00') + 86400)
        {
          $student = $this->user_model->student_info($event->student_id);

          my_send_mail(
            array(
              'to' => $student->mail,
              'subject' => "COURTESY REMINDER | {$this->config->item('school_name')}",
              'message' => "
<p>Hi {$student->first_name},</p>
<p>We just want to let you know that your current term is completed and is due for renewal.
To secure your time slot, please ensure your payment is received 72 hours after your last paid class.
If we have not received your payment 72 hours after your last paid class you will unfortunately forfeit your time slot.</p>
<p>Thank you, </p>
<p><strong>{$this->config->item('school_name_long')}</strong></p>
              "
            )
          );
        }
      } // end for($event_ids as $eid)

      // 24 hour auto-booking reminder response prior to any PAID and CONFIRMED scheduled event that they have lesson
      $schedule_limits = array(strtotime('+1 day midnight'), strtotime('+2 day midnight'), Schedule_model::STATUS_PENDING);
      $sql = "SELECT st.mail, st.first_name, u.first_name as teacher_first_name, stu.name, stu.street, stu.city, stu.postcode,
      s.sequence, s.start, s.end, u.mobile as teacher_mobile
      FROM schedules s
      INNER JOIN events e ON s.eid = e.eid
      INNER JOIN students st ON st.sid = e.student_id
      INNER JOIN users u ON u.uid = s.staff_id
      INNER JOIN studios stu ON s.studio_id = stu.sid
      WHERE s.start > ? AND s.start < ?
      AND s.status = ?";
      $result = $this->db->query($sql, $schedule_limits)->result();
      foreach($result as $row)
      {
        my_send_mail(
          array(
            'to' => $row->mail,
            'subject' => "REMINDER | {$this->config->item('school_name')}",
            'message' => "<p>Hi {$row->first_name},</p>
<p>You have a reservation with:</p>
<p><strong>Teacher:</strong> {$row->teacher_first_name}</p>
<p><strong>Mobile:</strong> {$row->teacher_mobile}</p>
<p><strong>Studio: </strong> {$row->street} {$row->city} {$row->postcode}</p>
<p><strong>Lesson {$row->sequence}</strong></p>
<p><strong>Start: " . date('d/m/Y g:ia', $row->start) . " - End: " . date('d/m/Y g:ia', $row->end) . "</strong></p>
<p>Thank You!</p>
<p><strong>{$this->config->item('school_name_long')}</strong></p>
          "
          )
        );
      }
    } // end if($current_hour == $this->config->item('voxcrm_mail_time'))
  }
}
